import { defineStore } from 'pinia'
import { ref } from 'vue'
import { courseApi } from '@/api'

export const useCourseStore = defineStore('course', () => {
  // 状态
  const courses = ref([])
  const total = ref(0)
  const loading = ref(false)
  const searchForm = ref({
    cno: '',
    cname: '',
    teacher: ''
  })
  const pagination = ref({
    page: 1,
    size: 10
  })
  const courseStats = ref({})

  // 获取课程列表
  const fetchCourses = async (params = {}) => {
    loading.value = true
    try {
      const queryParams = {
        ...searchForm.value,
        ...pagination.value,
        ...params
      }
      
      const response = await courseApi.getCourses(queryParams)
      if (response.code === 200) {
        courses.value = response.data.records || []
        total.value = response.data.total || 0
        return true
      }
      return false
    } catch (error) {
      console.error('获取课程列表错误:', error)
      return false
    } finally {
      loading.value = false
    }
  }

  // 添加课程
  const createCourse = async (courseData) => {
    try {
      const response = await courseApi.addCourse(courseData)
      if (response.code === 200) {
        await fetchCourses() // 刷新列表
        return { success: true, message: '添加课程成功' }
      }
      return { success: false, message: response.message || '添加课程失败' }
    } catch (error) {
      console.error('添加课程错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 更新课程
  const modifyCourse = async (courseId, courseData) => {
    try {
      const response = await courseApi.updateCourse(courseId, courseData)
      if (response.code === 200) {
        await fetchCourses() // 刷新列表
        return { success: true, message: '更新课程成功' }
      }
      return { success: false, message: response.message || '更新课程失败' }
    } catch (error) {
      console.error('更新课程错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 删除课程
  const removeCourse = async (courseId) => {
    try {
      const response = await courseApi.deleteCourse(courseId)
      if (response.code === 200) {
        await fetchCourses() // 刷新列表
        return { success: true, message: '删除课程成功' }
      }
      return { success: false, message: response.message || '删除课程失败' }
    } catch (error) {
      console.error('删除课程错误:', error)
      return { success: false, message: '网络错误，请稍后重试' }
    }
  }

  // 获取课程统计
  const fetchCourseStats = async (courseId) => {
    try {
      const response = await courseApi.getCourseStats(courseId)
      if (response.code === 200) {
        courseStats.value = response.data
        return true
      }
      return false
    } catch (error) {
      console.error('获取课程统计错误:', error)
      return false
    }
  }

  // 重置搜索条件
  const resetSearch = () => {
    searchForm.value = {
      cno: '',
      cname: '',
      teacher: ''
    }
    pagination.value.page = 1
  }

  // 设置分页
  const setPagination = (page, size) => {
    pagination.value.page = page
    pagination.value.size = size
  }

  return {
    // 状态
    courses,
    total,
    loading,
    searchForm,
    pagination,
    courseStats,
    
    // 方法
    fetchCourses,
    createCourse,
    modifyCourse,
    removeCourse,
    fetchCourseStats,
    resetSearch,
    setPagination
  }
})